package com.example.dao.impl;

import com.example.dao.CommentDao;
import com.example.entity.customerAdmin.Comment;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements CommentDao {
    @Override
    public List<Comment> selectAll () {
        List<Comment> commentList =new ArrayList<> ();
        ResultSet resultSet = BaseDao.select ("select * from comment ORDER BY time DESC", null);
        try {
            while (resultSet.next ()){
                Comment comment=new Comment ();
                comment.setName (resultSet.getString ("name"));
                comment.setTime (resultSet.getString ("time"));
                comment.setCusName (resultSet.getString ("cusName"));
                comment.setComment (resultSet.getString ("commentText"));
                commentList.add (comment);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return commentList;
    }

    @Override
    public int add (Comment comment) {
        int i=-1;
        Object[] objects=new Object[4];
        objects[0]=comment.getName ();
        objects[1]=comment.getTime ();
        objects[2]=comment.getCusName ();
        objects[3]=comment.getComment ();
        try {
            i= BaseDao.modify ("insert into comment set(name,time,cusName,commentText) value(?,?,?,?)", objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public Comment selectName (String name) {
        Comment comment=new Comment ();
        Object[] objects=new Object[1];
        objects[0]=comment.getName ();
        ResultSet select = BaseDao.select ("select * from comment where name=?", objects);
        try {
            while (select.next ()){
                comment.setId (select.getInt ("id"));
                comment.setName (select.getString ("name"));
                comment.setTime (select.getString ("time"));
                comment.setCusName (select.getString ("cusName"));
                comment.setComment (select.getString ("commentText"));
                return comment;
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return null;
    }

    @Override
    public int del (int id) {
        int i=-1;
        Object[] objects=new Object[1];
        objects[0]=id;
        try {
            i=BaseDao.modify ("delete from comment where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }
}
